public without sharing class SalesOrderDetailedHandler {
    
    //1、当海外销售订单明细新建编辑保存时，将客户价目表中的合作价、买赠价、样机价
    //分别带入对应客户海外销售订单明细的合作价(美元)、买赠价、样机价中；

    public static void getPriceToDetail(List<SalesOrderDetailed__c> list_new ){

        Map<Id,Set<Id>> sdMap = new Map<Id,Set<Id>>();
        Map<String,SalesOrderDetailed__c> sdetailMap = new Map<String,SalesOrderDetailed__c>();
        for(SalesOrderDetailed__c sd : list_new){

            Set<Id>details = sdMap.containsKey(sd.SalesOrderApplication__c) ? sdMap.get(sd.SalesOrderApplication__c) : new Set<Id>();
            details.add(sd.Product__c);
            sdMap.put(sd.SalesOrderApplication__c, details);
            if(sd.Product__c != null )sdetailMap.put(sd.SalesOrderApplication__c+'-'+sd.Product__c, sd);


        }
        Map<Id,Id> soaMap = new Map<Id,Id>();
        for(SalesOrderApplication__c soa : [select Id,Buyer__c from SalesOrderApplication__c where Id in:sdMap.keySet() ]){

            if(soa.Buyer__c  != null ) soaMap.put(soa.Id ,soa.Buyer__c);
        }
        if(soaMap.keySet().size() == 0 ) return;
        ///批量更新可能有问题
        for(Id soaId : sdMap.keySet() ){

            if(!soaMap.containsKey(soaId))continue;

            for(PriceList__c pl : [select Product__c , CooperativePrice__c,BuyingPrice__c, PrototypePrice__c from PriceList__c where Account__c =:soaMap.get(soaId) and Product__c in: sdMap.get(soaId)  ]){

                if(sdetailMap.containsKey(soaId+'-'+pl.Product__c)){
                    SalesOrderDetailed__c sd = sdetailMap.get(soaId+'-'+pl.Product__c);
                    sd.PrototypePrice__c = pl.PrototypePrice__c;
                    sd.BuyingPrice__c = pl.BuyingPrice__c;
                    sd.UnitPrice__c = pl.CooperativePrice__c;
                }

            }
        }

    }
}